package cn.javaxc.mvcwork.framework.mybatis;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.List;
import java.util.*;


import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;

import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.mapping.ParameterMode;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.executor.statement.BaseStatementHandler;
import org.apache.ibatis.executor.statement.RoutingStatementHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.mapping.ResultMap;
import org.apache.ibatis.mapping.ResultMapping;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.executor.resultset.DefaultResultSetHandler;
import org.apache.ibatis.executor.resultset.ResultSetHandler;
import cn.javaxc.mvcwork.util.helper.ReflectHelper;
import cn.javaxc.mvcwork.controller.MVCConfig;
import cn.javaxc.model.base.SQLMap;

@Intercepts({ @Signature(type = ResultSetHandler.class, method = "handleResultSets", args = { Statement.class }) })
public class ResultSetHandlerInterceptor implements Interceptor {
    
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        DefaultResultSetHandler resultSetHandler = (DefaultResultSetHandler) invocation.getTarget();
		MappedStatement mappedStatement = (MappedStatement) ReflectHelper.getValueByFieldName(resultSetHandler, "mappedStatement");
		String sqlid=mappedStatement.getId();
        Object result = invocation.proceed();
		Map namesMap=new HashMap();
//		if (mappedStatement.getResultMaps()!=null)
//		for (ResultMap map:mappedStatement.getResultMaps()){
//			System.err.println(this.getClass().getName()+" "+map.getId()+" "+map.getType());
//		}
		namesMap.put("code","code");
		namesMap.put("description","description");
		if (mappedStatement.getConfiguration().getResultMapNames().contains(sqlid+"_columns")){
			ResultMap resultMap=mappedStatement.getConfiguration().getResultMap(sqlid+"_columns");
			for (ResultMapping map:resultMap.getResultMappings() ){
				namesMap.put(map.getProperty(),map.getColumn());
			}
		}
		if (result instanceof List){
			List list=(List)result;
			if (list.size()>0&&list.get(0) instanceof SQLMap){
				for (SQLMap emt:(List<SQLMap>)list){
					if (emt==null){
						throw new RuntimeException(""+list.indexOf(emt));
					}
					if (emt.get(namesMap.get("code"))!=null){
						emt.setCode(emt.get(namesMap.get("code")));
					}
					if (emt.get(namesMap.get("description"))!=null){
						emt.setDescription(emt.get(namesMap.get("description"))+"");
					}
				}
			}
		}


        return result;
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {
    }

}
